<?php

/**
 * 得到用户的角色信息
 * 
 * @version 0.1
 * @date 2013-10-28
 * @author jinyue.wang
 */
class ZRole {

    /**
     * 得到登陆的session 信息
     * 
     * @return array()
     * 
     * @version 1.0
     * @date 2013-10-28
     */
    public static function getLoginUser() {
        //$redisSession = new ZRedisSession();
        //$data = $redisSession->getAll();
        $data = Yii::app()->user->getState('cuser');

        $info = null;
        if(!empty($data)){
            $info['cuser'] = $data;
        }
        /*if ($data != null && isset($data['cuser'])) {
            $info['cuser'] = unserialize($data['cuser']);
        }*/

        return $info;
    }

    /**
     * 得到当前学校对应的考试
     * 
     * @version 1.0
     * @date 2014-01-27
     */
    public function _getSchoolExam($aa_school_list){
        $exam_id_list = array();
        if(!empty($aa_school_list)){
            $sql = 'SELECT "exam_id" FROM "{{exam_school}}" WHERE "school_id" IN('.$aa_school_list.')';
            $result = Yii::app()->db->createCommand($sql);
            $row = $result->queryAll();
            if(!empty($row)){
                foreach($row as $value){
                    $exam_id_list[] = $value['exam_id'];
                }
            }
        }
        return $exam_id_list;
    }
    /**
     * 得到登陆的用户信息
     * 
     * @param $aa_user_sn
     * @param $aa_school_sn
     * @param $crole
     * @param $cclass
     * @param $cchild
     * 
     * @return array()
     * 
     * @version 1.0
     * @date 2013-10-28
     */
    public static function setLoginUser($aa_user_sn) {
        if (empty($aa_user_sn))
            return false;

        $zUser = new ZUser();

        $userInfo = $zUser->_getUserInfo($aa_user_sn);

        if ($userInfo == null)
            return false;

        $sql = 'SELECT "school_id","subject_id" FROM "{{user_school}}" WHERE "user_id" = :user_id ';
        $result = Yii::app()->db->createCommand($sql);
        $result->bindParam(':user_id',$aa_user_sn,PDO::PARAM_STR);
        $row  = $result->queryRow();
        //$redisSession = new ZRedisSession();
        $cUser = array(
            'id' => $aa_user_sn,
            'username' => $userInfo['username'],
            'role_id'    => $userInfo['role_id'],
            'school'     => $row
        );
        //$redisSession->setObject("cuser", $cUser);

        return $cUser;
    }
    /**
     * 判断角色是否有访问权限
     * 
     * $config=【action=>value】 value是数组的时候 角色验证
     * 是数字的时候为登录判断 1 登录 ， 0 为不许要验证
     * 
     * @param type $config array('action'=>array(rolelist))
     * @return type
     * 
     */
    public static function auth($config){
        $app=Yii::app()->controller;

        $action = strtolower($app->action->id);
        
        $cLoginUser = Z::getLoginUser();
        $crole = isset($cLoginUser['cuser']['role_id'])?$cLoginUser['cuser']['role_id']:0;
        
        $accessRole = isset($config[$action])?$config[$action]:0;
        
        // 如果设置了访问权限 
        if(is_array($accessRole) && !in_array($crole, $accessRole)){
             $action = false; 
        }
        
        // 需要登录
        if($accessRole==1){
            if(!isset($cLoginUser['cuser'])){
                $action = false; 
            }
        }
        
        return array(
            array(
                'allow',
                'actions' => array(
                    $action
                )
            ),
            array(
                'deny',
                'users' => array(
                    '*'
                )
            )
        ); 
    }
    
    
    /**
     * 得到左侧导航链接
     * 
     * @param $user_id
     * @param $role_id
     * @param $role_array
     * 
     * @return array
     * 
     * @version 1.0
     * @date 2013-12-15
     * @author jinyue.wang
     */
    public static function _getUserMenu($user_id,$role_id,$role_array){
        
        $conn = Yii::app()->db;
        
        $data = $new_data = $return_data = array();
        
        $sql = 'SELECT "id","ename","cname","parentid","m","c","a" FROM "{{menu}}" ';
        $sql .= ' WHERE "display" = 1 ORDER BY "listorder" ASC ';
        
        $result = $conn->createCommand($sql);
        $row = $result->queryAll();

        if (!empty($row)) {
            if ($role_id != 1) {
                foreach ($row as $value) {
                    if (in_array($value['id'], $role_array)) {
                        if ($value['parentid'] == 0) {
                            $data[] = $value;
                        } else {
                            $new_data[$value['parentid']][] = $value;
                        }
                    }
                }
            } else {
                foreach ($row as $value) {
                    if ($value['parentid'] == 0) {
                        $data[] = $value;
                    } else {
                        $new_data[$value['parentid']][] = $value;
                    }
                }
            }
            $result_data['parent'] = $data;
            $result_data['son'] = $new_data;
        }
        
        return $result_data;
    }
    
}